package cn.com.text;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.PreparedStatement;

import cn.com.text.Util.MySQLConnections;

/**
 * Created by 86176 on 2021/12/20.
 */

public class NewsContentFragment extends Fragment{
    private Connection connection;
    private View view;
    private FloatingActionButton floatingActionButton;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.news_content_frag,container,false);
        return view;
    }
    public void refresh(final String newsTitle, String newsContent){
        View visibilityLayout = view.findViewById(R.id.visibility_layout);
        visibilityLayout.setVisibility(View.VISIBLE);
        //TextView newTitleText = (TextView) view.findViewById(R.id.news_title);
        TextView newContentText = (TextView) view.findViewById(R.id.news_content);
        //newTitleText.setText(newsTitle);
        newContentText.setText(newsContent);
        floatingActionButton = (FloatingActionButton) view.findViewById(R.id.floatingActionButtonContent);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            connection = MySQLConnections.getConnection("blog");
                            String sql = "update Blog set content='"+"Hello World"+"' where title='"+newsTitle+"'";
                            PreparedStatement pst;
                            pst = connection.prepareStatement(sql);
                            pst.executeUpdate();
                            pst.close();
                            connection.close();
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
    }
}
